<template>
  <div>
    <el-form :model="numberValidateForm" ref="numberValidateForm" label-width="100px" class="demo-ruleForm" style="margin-top: 300px;margin-left: 200px">
      <el-form-item
          label="手机号"
          prop="phone"
          ref="phoneVerify"
          :rules="[
      { required: true, message: '手机号不能为空'},
      { type: 'number', message: '手机号必须为数字值'}
    ]"
      >
        <el-input type="phone" v-model.number="numberValidateForm.phone" autocomplete="off" style="width: 50%" id="phone"></el-input>&nbsp;&nbsp;<el-button type="primary" @click="getVerifyCode" :inline="true">获取验证码</el-button>
      </el-form-item>
      <el-form-item
          label="验证码"
          prop="verifyCode"
          :rules="[
      { required: true, message: '验证码不能为空'}
    ]"
      >
        <el-input v-model.number="numberValidateForm.verifyCode" autocomplete="off" style="width: 50%"></el-input>
        <font color="red">{{msg}}</font>
      </el-form-item>
      <el-form-item
          label="支付密码"
          prop="password"
          :rules="[
      { required: true, message: '支付密码不能为空'},
      { type: 'number', message: '支付密码必须为数字值'}
    ]"
      >
        <el-input type="number" v-model.number="numberValidateForm.password" autocomplete="off" style="width: 50%"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('numberValidateForm')">注册</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "register",
  data() {
    return {
      numberValidateForm: {
        phone: '',
        verifyCode: '',
        password: ''
      },
      code: '',
      msg: ''
    }
  },
  methods: {

    getVerifyCode() {
      this.$http.post('/backstage/user/getVerifyCode?phone='+this.numberValidateForm.phone).then( res => {
        this.code = res.data.data;
      })
    },

    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if(this.numberValidateForm.verifyCode == this.code){
            this.$http.post('/backstage/user/register',this.numberValidateForm).then( res => {
                console.log(res)
                window.location.href = 'http://server.smart-sso.com:8101';
            })
          }else{
            this.msg = "验证码错误";
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    }
  }
}
</script>

<style scoped>

</style>
